/*
 * ServerConfig.cpp
 *
 */

#include "ServerConfig.h"
#include <exception>

ServerConfig::ServerConfig(std::string nomArchXML) :
	Configuraciones(nomArchXML) {
}

ServerConfig::~ServerConfig() {
}

Port ServerConfig::getWebPort() const {
	return static_cast<Port> (this->nroPuerto);
}

Port ServerConfig::getCtrlPort() const {
	return static_cast<Port> (this->nroPuertoCtrl);
}

unsigned short int ServerConfig::getMaxSimulConn() const {
	return this->maxConexionesSimultaneas;
}

unsigned short int ServerConfig::getMaxClientConn() const {
	return this->maxConexionesCliente;
}

std::string ServerConfig::getRootPath() const {
	return analizePath(baseDir);
}

std::string ServerConfig::getStaticType(const std::string& extension) const {
	std::map<std::string, std::string>::const_iterator it;
	it = this->mapTiposEstaticos.find(extension);

	if (it == this->mapTiposEstaticos.end())
		throw std::string("Tipo estático no registrado");

	return it->second;
}

std::string ServerConfig::getDynamicType(const std::string& extension) const {
	std::map<std::string, std::string>::const_iterator it;
	it = this->mapTiposDinamicos.find(extension);

	if (it == this->mapTiposDinamicos.end())
		throw std::string("Tipo dinámico no registrado");

	return it->second;
}

std::string ServerConfig::getErrorHandler(const std::string& code) const {
	std::map<std::string, std::string>::const_iterator it;
	it = this->mapPaginasErrores.find(code);

	if (it == this->mapPaginasErrores.end())
		throw std::string("Código de error no registrado");
	if (it->second[0] == '/')
		return it->second;
	else {
		std::string s;
		s.push_back('/');
		s += it->second;
		return s;
	}
}

bool ServerConfig::getProtectionFlag() const {
	return this->autenticacionBasica;
}

bool ServerConfig::isUserAuthorized(const std::string& user,
		const std::string& pass) const {
	bool result = false;
	std::map<std::string, std::string>::const_iterator it;
	it = mapUsuarios.find(user);
	if (it != mapUsuarios.end() && it->second == pass)
		result = true;
	return result;
}

unsigned short int ServerConfig::getTimeout() const {
	return this->timeout;
}

std::string ServerConfig::getServerId() const {
	return this->serverId;
}

std::string ServerConfig::getPathLogAccesos() const {
	return pathLogAccesos;
}

std::string ServerConfig::getPathLogErrores() const {
	return pathLogErrores;
}

std::string ServerConfig::analizePath(const std::string& path) const {
	if (path[path.length()] == '/')
		return path.substr(0, path.length() - 1);
	else
		return path;
}
